# -*- coding:utf-8 -*-

# @Time    : 2023/5/16 02:21
# @Author  : zengwenjia
# @Email   : zengwenjia@lingxi.ai
# @File    : generate_bot_dialogue.py
# @Software: LLM_internal

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
import sys
sys.path.append('../internal_server/')
import pandas as pd
import asyncio
from bot.financial_sales.sales import Sales
from bot.financial_sales.user import User
# from bot.financial_sales.user_suggestion import UserSuggestion
import uuid
from data_generate import utils
import traceback
# from agent import llm_agent
from bot.financial_sales.agent import base_agent as llm_agent
import sys
from openpyxl import Workbook, load_workbook
# from knowledge_base.knowledge_embedding import AZUREQuery
import random
import os
from data_generate.generate_financial_train_data import get_deal_data
import json
import tqdm
from datetime import datetime

#根据真实用户对话记录生成数据
async def bot_dialogue_by_truth_conversation(path, taget_num=100):
    train_data = []
    llm_agent.change_default_llm("gpt")
    # 百家姓
    surname = ["赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈", "褚", "卫", "蒋", "沈", "韩", "杨", "朱", "秦", "尤", "许", ]
    sex = ['先生', '女士']
    rights_list = ['已获得优惠券', '已获得降息', '无权益', '已获得提额', '已获得优惠券,已获得降息',
                   '已获得提额,已获得降息', '已获得优惠券,已获得提额', '已获得优惠券,已获得降息,已获得提额']
    rightCutRate = ['', '利息降幅大', '利息降到最低']
    cut_rate_day = ['降息0天到期', '降息1天到期', '降息2天到期', '降息3天到期', '降息4天到期', '降息5天到期',
                    '降息6天到期', '降息7天到期', '降息8天后到期']


    new_data = list()

    deal_results = get_deal_data(path)
    print(deal_results)
    # results = deal_results[18:20]
    results = deal_results
    try:
        for result in tqdm.tqdm(results):

            session_id = str(result['session_id'])
            # if session_id in session_id_list:
            #     continue
            print(session_id)

            name = random.choice(surname) + random.choice(sex)
            age = random.choice(range(20, 70))
            right = random.choice(rights_list)
            cutRate = ''
            cutReteDay = ''
            if '已获得降息' in right:
                cutRate = random.choice(rightCutRate)
                cutReteDay = random.choice(cut_rate_day)
            # base_info = {'用户年龄': str(age), '初始用户权益': right}
            base_info = {'用户姓名': name, '用户年龄': str(age), '初始用户权益': right, '降息幅度': cutRate,
                         '降息剩余天数': cutReteDay, 'case_info': '之前在忙', }
            context = []
            llm_value = ''
            user_question = ''
            for conversation in result["conversations"]:

                conversation_dict = {}
                if conversation["role"] == "销售员":
                    conversation_dict["role"] = "assistant"
                    value = conversation["value"]
                    sign = conversation["sign"]
                    conversation_dict["content"] = value
                    query_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                    conversation_dict["query_time"] = query_time
                    conversation_dict["user_question"] = user_question

                    context.append(conversation_dict)

                    if sign:
                        if sign == '挂机':
                            sign_result = True if '再见' in llm_value else False
                        elif sign == '非挂机':
                            sign_result = True if '再见' not in llm_value else False
                        elif sign == '加微信':
                            sign_result = True if '我用官方企业微信添加一下您，' in llm_value else False
                        elif sign == '工号':
                            sign_result = True if '工号' in llm_value else False
                        else:
                            sign_result = ''
                    else:
                        sign_result = ''

                    new_data.append({
                        'role': '销售员',
                        'content': value,
                        'llm_value': llm_value,
                        'result': sign_result,
                    })
                    if "再见" in value or "拜拜" in value:
                        break


                elif conversation["role"] == "用户":
                    conversation_dict["role"] = "user"
                    conversation_dict["content"] = conversation["value"]
                    query_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                    conversation_dict["query_time"] = query_time
                    context.append(conversation_dict)

                    bot = Sales()
                    res = await bot.async_reply(context, session_id, base_info)
                    result = res['reply_text']
                    user_question = res['user_question']
                    user_question_prompt = res['user_question_prompt']
                    sales_skill_name = res['sales_skill_name']
                    sales_skill_name_prompt = res['sales_skill_name_prompt']
                    solution = res['solution']
                    solution_prompt = res['solution_prompt']
                    conversation_prompt = res['conversation_prompt']
                    print(res)
                    new_data.append({
                        'role': '用户',
                        'content': conversation["value"],
                        'llm_value': '',
                        'result': ''
                    })
                    llm_value = result

                    # if "再见" in result["reply_text"] or "拜拜" in result["reply_text"]:
                    #     break

            new_data.append({
                'role': '',
                'content': '',
                'llm_value': '',
                'result': ''
            })

        new_datas = pd.DataFrame(new_data)
        new_datas.to_csv('./不能错case结果.csv', index=False)



    except Exception as e:
        traceback.print_exc()
        # 打印堆栈
        print(e)


#用户query模拟生成
async def mock_bot_dialogue_by_query(path, taget_num=1):
    train_data = []
    llm_agent.change_default_llm("gpt")
    #百家姓
    surname = ["赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈", "褚", "卫", "蒋", "沈", "韩", "杨", "朱", "秦", "尤", "许",]
    sex = ['先生', '女士']
    rights_list = ['已获得优惠券', '已获得降息', '无权益', '已获得提额']

    all_conversation_template, user_questions = get_conversation_template(["../deal_data/2144_10_30_成交对话.csv"])
    # faq_corpus = get_faq_corpus("../data/222.csv")
    faq_corpus = list()
    user_questions = user_questions + faq_corpus

    out_writer = open("dialogue_tmp.txt", 'a+')
    for i in range(taget_num):
        name = random.choice(surname) + random.choice(sex)
        age = random.choice(range(20, 70))
        right = random.choice(rights_list)
        try:
            bot = Sales()
            context = []
            instruct_dict_new = {}
            session_id = str(uuid.uuid1())
            instruct_dict_new["id"] = session_id
            instruct_dict_new["messages"] = []
            instruct_dict_new["messages"].append({
                "role": "user",
                "content": "你好。"
            })
            base_info = {'用户姓名': name, '用户年龄': str(age), '初始用户权益': right}

            random.shuffle(user_questions)
            condidate_query = '\n'.join(user_questions[:100])

            history = bot.format_conversation_history(context)
            res = await bot.async_reply(history, session_id, base_info)

            result = res['reply_text']
            user_question = res['user_question']
            user_question_prompt = res['user_question_prompt']
            sales_skill_name = res['sales_skill_name']
            sales_skill_name_prompt = res['sales_skill_name_prompt']
            solution = res['solution']
            solution_prompt = res['solution_prompt']
            conversation_prompt = res['conversation_prompt']


            conversation_dict = {}
            conversation_dict["role"] = "assistant"
            conversation_dict["content"] = result
            conversation_dict["content_prompt"] = conversation_prompt
            conversation_dict["user_question"] = user_question
            conversation_dict["user_question_prompt"] = user_question_prompt
            conversation_dict["sales_skill_name"] = sales_skill_name
            conversation_dict["sales_skill_name_prompt"] = sales_skill_name_prompt
            conversation_dict["solution"] = solution
            conversation_dict["solution_prompt"] = solution_prompt

            instruct_dict_new["messages"].append(conversation_dict)
            context.append(conversation_dict)

            for j in range(10):
                history = bot.format_conversation_history(context)

                user = User(condidate_query, history)
                query = await user.achat_with_proxy_gpt4(save_data=False)
                conversation_dict = {}
                conversation_dict["role"] = "user"
                conversation_dict["content"] = query.strip()
                instruct_dict_new["messages"].append(conversation_dict)
                context.append(conversation_dict)
                mute_count = 0
                for conversation in context:
                    if conversation['role'] == 'user' and conversation['content'] == '@@quiet@@':
                        mute_count += 1
                user_question = ''
                user_question_prompt = ''
                sales_skill_name = ''
                sales_skill_name_prompt = ''
                solution = ''
                solution_prompt = ''
                conversation_prompt = ''
                if mute_count >= 3:
                    res = '那等您方便的时候我们再联系吧，祝您生活愉快，再见！'
                    result = res
                elif query == '@@quiet@@':
                    res = '抱歉哈，没太听清楚，您是本人吗？'
                    result = res
                else:
                    bot = Sales()
                    res = await bot.async_reply(context, session_id, base_info)
                    result = res['reply_text']
                    user_question = res['user_question']
                    user_question_prompt = res['user_question_prompt']
                    sales_skill_name = res['sales_skill_name']
                    sales_skill_name_prompt = res['sales_skill_name_prompt']
                    solution = res['solution']
                    solution_prompt = res['solution_prompt']
                    conversation_prompt = res['conversation_prompt']
                    # res = res["reply_text"]

                conversation_dict = {}
                conversation_dict["role"] = "assistant"
                conversation_dict["content"] = result
                conversation_dict["content_prompt"] = conversation_prompt
                conversation_dict["user_question"] = user_question
                conversation_dict["user_question_prompt"] = user_question_prompt
                conversation_dict["sales_skill_name"] = sales_skill_name
                conversation_dict["sales_skill_name_prompt"] = sales_skill_name_prompt
                conversation_dict["solution"] = solution
                conversation_dict["solution_prompt"] = solution_prompt
                instruct_dict_new["messages"].append(conversation_dict)
                context.append(conversation_dict)

                # history = bot.format_conversation_history(context)

                # user_suggestion_obj = UserSuggestion(conversation_history=history)
                # user_suggestion = await user_suggestion_obj.achat_auto_llm()
                # print(user_suggestion)

                if "再见" in result or "拜拜" in result:
                    break
            train_data.append(instruct_dict_new)
            utils.jdump(train_data, path)
            out_writer.write(str(instruct_dict_new) + "\n")
            out_writer.flush()
        except Exception as e:
            traceback.print_exc()
            # 打印堆栈
            print(e)

    out_writer.close()


def get_faq_corpus(path):
    standard_expand = {}
    df = pd.read_csv(path)
    faqDictList = df.to_dict("records")
    for index, faq in enumerate(faqDictList):
        standard = faq['标准问']
        expand = faq['扩展问']
        if standard == "NOINTENT" or standard == "" or standard == "nan":
            continue
        if standard not in standard_expand:
            standard_expand[standard] = []
        standard_expand[standard].append(expand)

    all_query = []
    for k, v in standard_expand.items():
        random.shuffle(v)
        if len(v) < 50:
            all_query.extend(v)
        elif len(v) < 100:
            all_query.extend(v[:int(len(v) * 0.7)])
        elif len(v) < 200:
            all_query.extend(v[:int(len(v) * 0.6)])
        elif len(v) < 300:
            all_query.extend(v[:int(len(v) * 0.5)])
        elif len(v) < 400:
            all_query.extend(v[:int(len(v) * 0.4)])
        else:
            all_query.extend(v[:int(len(v)*0.2)])
    random.shuffle(all_query)

    # print(len(all_query))

    return all_query


#对话数据模拟用户生成
async def mock_bot_dialogue_by_user(path, taget_num=5):
    train_data = []
    llm_agent.change_default_llm("gpt")
    #百家姓
    surname = ["赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈", "褚", "卫", "蒋", "沈", "韩", "杨", "朱", "秦", "尤", "许",]
    sex = ['先生', '女士']

    all_conversation_template, user_questions = get_conversation_template(["../internal_server/deal_data/"])
    random.shuffle(all_conversation_template)
    use_conversation_template = all_conversation_template[:taget_num]

    out_writer = open("dialogue_tmp.txt", 'a+')
    for i in range(len(use_conversation_template)):
        name = random.choice(surname) + random.choice(sex)
        try:
            bot = Sales()
            context = []
            instruct_dict_new = {}
            session_id = str(uuid.uuid1())
            instruct_dict_new["id"] = session_id
            instruct_dict_new["messages"] = []
            instruct_dict_new["messages"].append({
                "role": "user",
                "content": "你好。"
            })
            base_info = []


            history = bot.format_conversation_history(context)
            res = await bot.async_reply(name, history, session_id, base_info)
            conversation_dict = {}
            conversation_dict["role"] = "assistant"
            conversation_dict["content"] = res
            instruct_dict_new["messages"].append(conversation_dict)
            context.append(conversation_dict)

            for j in range(8):
                bot = Sales()
                history = bot.format_conversation_history(context)
                user = User(use_conversation_template[i], history)
                query = await user.achat_with_proxy_gpt4(save_data=False)
                conversation_dict = {}
                conversation_dict["role"] = "user"
                conversation_dict["content"] = query.replace("<end>", "")
                instruct_dict_new["messages"].append(conversation_dict)
                context.append(conversation_dict)

                # history = bot.planner.format_conversation_history(context)
                res = await bot.async_reply(name, context, session_id, base_info)
                conversation_dict = {}
                conversation_dict["role"] = "assistant"
                conversation_dict["content"] = res
                instruct_dict_new["messages"].append(conversation_dict)
                context.append(conversation_dict)

                # history = bot.format_conversation_history(context)

                # user_suggestion_obj = UserSuggestion(conversation_history=history)
                # user_suggestion = await user_suggestion_obj.achat_auto_llm()
                # print(user_suggestion)

                if "<end>" in query:
                    break
            train_data.append(instruct_dict_new)
            utils.jdump(train_data, path)
            out_writer.write(str(instruct_dict_new) + "\n")
            out_writer.flush()
        except Exception as e:
            traceback.print_exc()
            # 打印堆栈
            print(e)

    out_writer.close()


def get_conversation_template(path):
    datas = []
    user_contents = []
    for csv_path in path:
        if os.path.isdir(csv_path):
            for file in os.listdir(csv_path):
                if not file.endswith('.csv'):
                    continue
                df = pd.read_csv(csv_path + file)
                faqDictList = df.to_dict('records')
                datas.extend(faqDictList)
        else:
            df = pd.read_csv(csv_path)
            faqDictList = df.to_dict('records')
            datas.extend(faqDictList)

    all_sessions = {}
    for index, faq in enumerate(datas):
        sessionId = str(faq['sessionId'])
        if sessionId not in all_sessions:
            all_sessions[sessionId] = []
            all_sessions[sessionId].append({"role": "用户", "value": "你好。"})
        all_sessions[sessionId].append({"role": str(faq['角色']), "value": str(faq['过滤文本'])})

    all_sessions_new = {}
    for session_id, session_value in all_sessions.items():
        new_session_value = []
        i = 0
        while i < len(session_value):
            j = i + 1
            while j < len(session_value):
                if session_value[j]['role'] == session_value[i]['role']:
                    session_value[i]['value'] += session_value[j]['value']
                elif session_value[i]['role'] == '用户':
                    new_session_value.append(session_value[i])
                    user_contents.append(session_value[i]['value'])
                    new_session_value.append(session_value[j])
                    break
                j += 1
            i = j + 1
        if len(new_session_value) > 8:
            all_sessions_new[session_id] = new_session_value

    # print(all_sessions_new)
    all_conversations = []
    for _id, _conv in all_sessions_new.items():
        conversation_text = ""
        for c in _conv:
            query = c['role'] + ": " + c['value'] + "\n"
            conversation_text += query
        all_conversations.append(conversation_text)

    from collections import Counter
    user_contents_dict = dict(Counter(user_contents))
    user_contents_dict = sorted(user_contents_dict.items(), key=lambda x:x[1], reverse=True)
    print(user_contents_dict)
    with open("user_contents.txt", 'w') as f:
        for i in user_contents_dict[1:2001]:
            f.write(i[0] + "\n")

    user_questions = [i[0] for i in user_contents_dict[1:2001]]

    return all_conversations, user_questions


# gpt4模拟用户生成bot对话
async def mock_bot_dialogue(path, taget_num=20):
    train_data = []
    llm_agent.change_default_llm("gpt")
    #百家姓
    surname = ["赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈", "褚", "卫", "蒋", "沈", "韩", "杨", "朱", "秦", "尤", "许",]
    sex = ['先生', '女士']

    out_writer = open("dialogue_tmp.txt", 'a+')
    for i in range(taget_num):
        name = random.choice(surname) + random.choice(sex)
        try:
            bot = Sales()
            context = []
            instruct_dict_new = {}
            session_id = str(uuid.uuid1())
            instruct_dict_new["id"] = session_id
            instruct_dict_new["messages"] = []
            instruct_dict_new["messages"].append({
                "role": "user",
                "content": "你好。"
            })
            base_info = []


            history = bot.format_conversation_history(context)
            res = await bot.async_reply(name, history, session_id, base_info)
            conversation_dict = {}
            conversation_dict["role"] = "assistant"
            conversation_dict["content"] = res
            instruct_dict_new["messages"].append(conversation_dict)
            context.append(conversation_dict)

            for i in range(8):
                history = bot.format_conversation_history(context)
                user = User(history)
                query = await user.achat_with_proxy_gpt4(save_data=False)
                conversation_dict = {}
                conversation_dict["role"] = "user"
                conversation_dict["content"] = query.replace("<end>", "")
                instruct_dict_new["messages"].append(conversation_dict)
                context.append(conversation_dict)

                # history = bot.planner.format_conversation_history(context)
                res = await bot.async_reply(name, context, session_id, base_info)
                conversation_dict = {}
                conversation_dict["role"] = "assistant"
                conversation_dict["content"] = res
                instruct_dict_new["messages"].append(conversation_dict)
                context.append(conversation_dict)

                history = bot.format_conversation_history(context)

                # user_suggestion_obj = UserSuggestion(conversation_history=history)
                # user_suggestion = await user_suggestion_obj.achat_auto_llm()
                # print(user_suggestion)

                if "<end>" in query:
                    break
            train_data.append(instruct_dict_new)
            utils.jdump(train_data, path)
            out_writer.write(str(instruct_dict_new) + "\n")
            out_writer.flush()
        except Exception as e:
            traceback.print_exc()
            # 打印堆栈
            print(e)

    out_writer.close()


def add_empty_row(group):
    return pd.concat([group, pd.DataFrame([[''] * len(group.columns)], columns=group.columns)],
                     ignore_index=True)


def conversation2csv(file_path):
    datas = utils.jload(file_path)
    df = pd.DataFrame(columns=['角色', '内容'])

    for data in datas:
        messages = data['messages']
        for text in messages:
            df = df.append(
                {"角色": text['role'],
                 "内容": text['content']},
                ignore_index=True)
        df = df.reset_index(drop=True).append(
            {"角色": "",
             "内容": ""},
            ignore_index=True)
    df.to_csv("gen_conversation.csv")

#把csv文件转换成vicuna训练数据
def csv2vicuna(file_paths, target_path):
    train_datas = []
    for file_path in file_paths:
        df = pd.read_csv(file_path)
        # df = df.dropna()
        for index, row in df.iterrows():
            train_data = {}
            session_id = str(uuid.uuid1())
            conversations = []
            conversation = {}
            if pd.isna(row['prompt']) or pd.isna(row['修改后结果']):
                print(row)
                continue
            if '录音编号是' in row['prompt']:
                if "," not in row['修改后结果'] and row['修改后结果'] not in row['prompt']:
                    print(row)
                    continue
                elif "," in row['修改后结果']:
                    results = row['修改后结果'].split(",")
                    not_in_flag = False
                    for result in results:
                        if result not in row['prompt']:
                            not_in_flag = True
                    if not_in_flag:
                        print(row)
                        continue

            original_result = row['原结果']

            conversation['from'] = 'human'
            conversation['value'] = row['prompt']
            conversations.append(conversation)
            conversation = {}
            conversation['from'] = 'gpt'
            conversation['value'] = row['修改后结果']
            conversations.append(conversation)
            train_data['id'] = session_id
            train_data['conversations'] = conversations
            train_datas.append(train_data)
    utils.jdump(train_datas, target_path)


def csv2reward(file_path, target_path):
    df = pd.read_csv(file_path)
    # df = df.dropna()
    datas = []
    for index, row in df.iterrows():
        session_id = str(uuid.uuid1())
        if pd.isna(row['prompt']) or pd.isna(row['修改后结果']):
            print(row)
            continue
        if '录音编号是' in row['prompt'] and row['修改后结果'] not in row['prompt']:
            print(row)
            continue

        prompt = row['prompt']
        original_result = row['原结果']
        change_result = row['修改后结果']

        # ColossalAI
        '''datas.append({
            'chosen': prompt + str(change_result),
            'rejected': prompt + str(original_result),
        })'''
        # deepspeed
        datas.append({
            'prompt': 'Human: '+prompt+'\n Assistant:',
            'chosen': str(change_result),
            'rejected': str(original_result),
        })

    random.shuffle(datas)
    print('数据量:{}'.format(len(datas)))
    print(len(datas) // 10)
    train_data = datas[:len(datas) // 10 * 9]
    eval_data = datas[len(datas) // 10 * 9:]
    print(len(train_data))
    print(len(eval_data))

    utils.jdump(train_data, target_path+'_train.json')
    utils.jdump(eval_data, target_path+'_eval.json')


# 修改训练数据
def modify_train_data(path, target_path):
    sales_skill_dict = {
        "核实用户身份": "核实身份",
        "开场表明身份来意": "表明来意",
        "介绍产品活动优惠": "介绍活动",
        "“介绍产品活动优惠”": "介绍活动",
        "带操作：": "引导操作：",
        "询问用户是否有App": "询问有无App",
        "“询问用户是否有App”": "询问有无App",
        "询问用户是否有京东金融App": "询问有无京东金融Apfp",
        "询问用户是否打开了App": "引导打开App",
        "“带操作-询问用户是否打开了App”": "引导打开App",
        "询问用户是否找到了“金条借款”几个字": "引导找到金条借款",
        "询问用户是否找到了'金条借款'几个字": "引导找到金条借款",
        "询问用户是否找到了金条借款几个字": "引导找到金条借款",
        "询问用户是否找到了”金条借款“几个字": "引导找到金条借款",
        "询问用户是否看到“我要借款”按钮": "引导找到借款按钮",
        "“询问用户是否看到“我要借款”按钮”": "引导找到借款按钮",
        "询问用户是否看到”我要借款“按钮": "引导找到借款按钮",
        "询问用户是否能看到“我要借款”页面内容": "引导点击借款按钮",
        "用户是否能看到“我要借款页面”内容": "引导点击借款按钮",

        "介绍“我要借款”页面选择用款期数和还款方式": "介绍借款页面",
        "建议用户借全额": "建议借全额",
        "加用户微信-意向确认": "加微意向确认",
        "加用户微信-申请提醒": "加微申请提醒",
        "销售策略的使用顺序是“核实用户身份”，“开场表明身份来意”，“介绍产品活动优惠”，“带操作”，“加用户微信”。其中“带操作”包括具体需要选择的子策略": "销售策略的使用顺序是“核实身份”，“表明来意”，“介绍活动”，“引导操作”，“加用户微信”。其中“引导操作”包括具体需要选择的子策略",
        "“带操作”":"“引导操作”"
    }

    datas = utils.jload(path)
    new_datas = []
    for data in datas:
        conversations = data['conversations']
        if len(conversations) == 2:
            prompt = conversations[0]['value']
            answer = conversations[1]['value']
        #     if "好的，这边给您转接客户经理一对一服务，您不要挂机哈" in prompt:
        #         prompt = str(prompt).replace("好的，这边给您转接客户经理一对一服务，您不要挂机哈", "")
        #     if "@@commonFaq@@，" in prompt:
        #         prompt = prompt.replace("@@commonFaq@@，", "")
        #     if "具体情况让客户经理给您说下，您不要挂机哈" in prompt:
        #         prompt = prompt.replace("具体情况让客户经理给您说下，您不要挂机哈", "")
        #
        #     # 根据sales_skill_dict进行prompt的内容替换
        #     if "“核实用户身份”，“开场表明身份来意”，“介绍产品活动优惠”，“带操作”，“加用户微信”" in prompt:
        #         for key in sales_skill_dict:
        #             prompt = str(prompt).replace(key, sales_skill_dict[key])
        #
        #         answer = str(answer).replace("\"", "")
        #         answer = str(answer).replace("'", "")
        #         answer = str(answer).replace("。", "").strip()
        #         if answer in sales_skill_dict:
        #             answer = str(answer).replace(answer, sales_skill_dict[answer])
        #         elif answer != "无销售策略":
        #             print(answer)
        #             print(path)
        #         conversations[0]['value'] = prompt
        #         conversations[1]['value'] = answer

            # elif "根据对话记录和用户信息列表，一步一步推理出的用户信息" in prompt:
            #     prompt = str(prompt).replace("输出格式为json格式：key: value格式。key为信息名，value为信息的值",
            #                                  "输出格式为通过\",\"拼接的value值，如：有京东APP,已打开APP，若只有一个有值，则返回该结果，空返回无")
            #     prompt = prompt.replace("（json格式）", "")
                # conversations[0]['value'] = prompt
                #
                # try:
                #     json_value = json.loads(answer)
                #     json_value_result = {}
                #     if "有无APP" in json_value and ("未知" not in json_value["有无APP"]):
                #         json_value_result["有无APP"] = json_value["有无APP"]
                #     if "是否已打开APP" in json_value and json_value["是否已打开APP"] == "已打开APP":
                #         json_value_result["是否已打开APP"] = json_value["是否已打开APP"]
                #     result_json = json_value_result
                # except Exception as e:
                #     # print(e)
                #     result_json = {}
                # if result_json:
                #     conversations[1]['value'] = ",".join(result_json.values())
                # else:
                #     conversations[1]['value'] = "无"
                #
                # data['conversations'] = conversations

            if ("（多个录音编号拼接用,分割）" in prompt) or ("（多个录音编号用,分割）" in prompt):
                for key in sales_skill_dict:
                    prompt = str(prompt).replace(key, sales_skill_dict[key])
                prompt = prompt.replace("带操作-", "")
                conversations[0]['value'] = prompt
                conversations[1]['value'] = answer
        new_datas.append(data)


    utils.jdump(new_datas, target_path)


def modify_csv_data(path, target_path):
    sales_skill_dict = {
        "核实用户身份": "核实身份",
        "开场表明身份来意": "表明来意",
        "介绍产品活动优惠": "介绍活动",
        "“介绍产品活动优惠”": "介绍活动",
        "带操作：": "引导操作：",
        "询问用户是否有App": "询问有无App",
        "“询问用户是否有App”": "询问有无App",
        "询问用户是否有京东金融App": "询问有无京东金融App",
        "询问用户是否打开了App": "引导打开App",
        "“带操作-询问用户是否打开了App”": "引导打开App",
        "询问用户是否找到了“金条借款”几个字": "引导找到金条借款",
        "询问用户是否找到了'金条借款'几个字": "引导找到金条借款",
        "询问用户是否找到了金条借款几个字": "引导找到金条借款",
        "询问用户是否找到了”金条借款“几个字": "引导找到金条借款",
        "询问用户是否看到“我要借款”按钮": "引导找到借款按钮",
        "“询问用户是否看到“我要借款”按钮”": "引导找到借款按钮",
        "询问用户是否能看到“我要借款”页面内容": "引导点击借款按钮",
        "介绍“我要借款”页面选择用款期数和还款方式": "介绍借款页面",
        "建议用户借全额": "建议借全额",
        "加用户微信-意向确认": "加微意向确认",
        "加用户微信-申请提醒": "加微申请提醒",
        "销售策略的使用顺序是“核实用户身份”，“开场表明身份来意”，“介绍产品活动优惠”，“带操作”，“加用户微信”。其中“带操作”包括具体需要选择的子策略": "销售策略的使用顺序是“核实身份”，“表明来意”，“介绍活动”，“引导操作”，“加用户微信”。其中“引导操作”包括具体需要选择的子策略",
        "“带操作”": "“引导操作”"
    }

    datas = pd.read_csv(path)
    new_datas = []
    for index, data in datas.iterrows():
        conversations = data
        prompt = conversations["prompt"]
        answer = conversations["修改后结果"]
        pre_answer = conversations["原结果"]
        if "好的，这边给您转接客户经理一对一服务，您不要挂机哈" in prompt:
            prompt = str(prompt).replace("好的，这边给您转接客户经理一对一服务，您不要挂机哈", "")
        if "@@commonFaq@@，" in prompt:
            prompt = prompt.replace("@@commonFaq@@，", "")
        if "具体情况让客户经理给您说下，您不要挂机哈" in prompt:
            prompt = prompt.replace("具体情况让客户经理给您说下，您不要挂机哈", "")

        # 根据sales_skill_dict进行prompt的内容替换
        if "“核实用户身份”，“开场表明身份来意”，“介绍产品活动优惠”，“带操作”，“加用户微信”" in prompt:
            for key in sales_skill_dict:
                prompt = str(prompt).replace(key, sales_skill_dict[key])

            answer = str(answer).replace("\"", "")
            answer = str(answer).replace("'", "")
            answer = str(answer).replace("。", "").strip()
            if answer in sales_skill_dict:
                answer = str(answer).replace(answer, sales_skill_dict[answer])
            elif answer != "无销售策略":
                print(answer)
                print(path)
            if pre_answer in sales_skill_dict:
                pre_answer = str(pre_answer).replace(pre_answer, sales_skill_dict[pre_answer])
            conversations["prompt"] = prompt
            conversations["修改后结果"] = answer
            conversations["原结果"] = pre_answer

        elif "根据对话记录和用户信息列表，一步一步推理出的用户信息" in prompt:
            prompt = str(prompt).replace("输出格式为json格式：key: value格式。key为信息名，value为信息的值",
                                         "输出格式为通过\",\"拼接的value值，如：有京东APP,已打开APP，若只有一个有值，则返回该结果，空返回无")
            prompt = prompt.replace("（json格式）", "")
            conversations["prompt"] = prompt

            try:
                json_value = json.loads(answer)
                json_value_result = {}
                if "有无APP" in json_value and ("未知" not in json_value["有无APP"]):
                    json_value_result["有无APP"] = json_value["有无APP"]
                if "是否已打开APP" in json_value and json_value["是否已打开APP"] == "已打开APP":
                    json_value_result["是否已打开APP"] = json_value["是否已打开APP"]
                result_json = json_value_result
            except Exception as e:
                # print(e)
                result_json = {}
            if result_json:
                conversations["修改后结果"] = ",".join(result_json.values())
            else:
                conversations["修改后结果"] = "无"
        elif ("（多个录音编号拼接用,分割）" in prompt) or ("（多个录音编号用,分割）" in prompt):
            for key in sales_skill_dict:
                prompt = str(prompt).replace(key, sales_skill_dict[key])
            prompt = prompt.replace("带操作-", "")
            conversations["prompt"] = prompt

        new_datas.append(conversations)
    new_datas = pd.DataFrame(new_datas)
    new_datas.to_csv(target_path, index=False)


#合并所有的训练数据
def merge_train_data(path, target_path):
    datas = []
    for file in os.listdir(path):
        if not file.endswith('.json'):
            continue
        data = utils.jload(path + file)
        if ("human_value_alignment_instructions.json" == file) or ("counterfactural_correction_multi_round_chat.json" == file) or ("bot_dialogue.json" == file) or ("cot_2023-09-02.json" == file):
            file_size = len(data)
            # 随机抽取1/3的数据
            data = random.sample(data, int(file_size / 4))
            print(len(data))
            data = data[:int(file_size / 4)]
            print(len(data))
        for record in data:
            new_record = {}
            conversations = record['conversations']
            if len(conversations) == 2:
                prompt = conversations[0]['value']
                answer = conversations[1]['value']
                if "（多个录音编号拼接用,分割）：" in prompt:
                    if len(answer) > 30:
                        print(answer)
                        continue
                if "好的，这边给您转接客户经理一对一服务，您不要挂机哈" in prompt:
                    prompt = str(prompt).replace("好的，这边给您转接客户经理一对一服务，您不要挂机哈", "")
                if "@@commonFaq@@，" in prompt:
                    prompt = prompt.replace("@@commonFaq@@，", "")
                if "具体情况让客户经理给您说下，您不要挂机哈" in prompt:
                    prompt = prompt.replace("具体情况让客户经理给您说下，您不要挂机哈", "")
                conversations[0]['value'] = prompt

                if ("1. 有无APP" in prompt) and ("输出格式为json格式" in prompt):
                    try:
                        print(answer)
                        json_value = json.loads(answer)
                        json_value_result = {}
                        if "有无APP" in json_value and ("未知" not in json_value["有无APP"]):
                            json_value_result["有无APP"] = json_value["有无APP"]
                        if "是否已打开APP" in json_value and json_value["是否已打开APP"] == "已打开APP":
                            json_value_result["是否已打开APP"] = json_value["是否已打开APP"]
                        json_value = json_value_result
                    except Exception as e:
                        print(e)
                        json_value = {}
                    conversations[1]['value'] = str(json_value)
            new_record['id'] = record['id']
            new_record['conversations'] = conversations
            datas.append(record)
    # 打乱datas
    random.shuffle(datas)
    print(len(datas))
    utils.jdump(datas, target_path + "all_train_data.json")


def check_train_data(paths):

    quesion_prompt = '''作为一个专业的、非常懂营销的信贷领域的销售员，你正通过电话和用户沟通，你需要根据当前的对话记录用简洁的一句短语总结用户的当前想要解决的问题或疑议。

需要注意以下要求：
1.如果用户表达是一句完整的话，不需要联系上文的销售员说的话，如果不是完整的，需要联系上文销售员说的话。
2.用户问题包括：非本人、询问什么平台、是否会查征信、利息多少，还款方式、提前还款、下款时间，不满投诉、投诉倾向、别给我打电话了，怎么有我的号码，询问工号，静音未回复，怎么注销，已经注销，打电话目的，不愿意参与兜底降息、提额、优惠券等活动，不会操作，通话中没有网络，为什么要操作，咨询京东其他产品，操作麻烦，不愿意加微，咨询兜底降息、提额、优惠券等产品/活动信息，是否有销售任务，挂机后自己操作，多次不需要，多次在忙等等；
3.如果用户当前没有问题，则输出"用户无问题"，不要无中生有。
4.用户表示已经提额成功或借款成功，表示用户无问题。
5.用户表述额度相关问题，询问额度多少，表示额度低，想提升额度，用户问题分别是额度多少，额度低，如何提升额度。
6.用户表示不能用，额度不能用，无法提供服务，综合评分不足等，用户问题是无法正常借款。
7.用户表示没有额度了、额度用完了等，用户问题是额度已用完。
8.用户询问利息多少，想降低利率，用户问题分别是利息多少，如何降低利率。
9.用户想要息费减免券、折扣券、优惠券、打折券、降低利率利息、觉得利息高、有利率不满问题等，用户问题是利率高。
10.用户表示是智能助手，语音助手，电话助理，请录音，录音超时等时，用户其实并非真人而是AI回复，这时用户问题是并非真人。
11.用户表示稍后查看，之后操作，之后会考虑，有需要再说等，用户问题是会考虑。用户肯定回应算是用户无问题。
12.用户表示会使用APP，会操作APP，知道操作流程，用户问题是自己会操作。
13.用户表示在忙，开车，上班等没有时间进行沟通时，用户问题是在忙。
14.用户未明白销售员的表述含义，或者希望销售员简明点表述，用户问题是没听懂。
15.用户想打断销售员、用户要求销售员暂停，用户问题是打断。
16.用户询问怎么去操作，去哪操作，怎么去查看，去哪查看，用户问题是如何操作。
17.用户询问是哪个APP，用户问题是申请渠道。
18.用户表述不需要，不用等拒绝含义，用户问题是不需要。用户可能会用不缺钱、使用其他平台等来表示自己不需要，这时应返回"不需要-不缺钱"、"不需要-使用其他平台"。用户一句话中超过3次表述不需要，不用，用户问题是强烈拒绝。
19.用户一句话中或上下文中超过3次表述不需要，不用，用户问题是强烈拒绝。用户一句话中或上下文中超过3次表述在忙、会考虑、稍后再说等相同意思，需要加上"多次表示"的前缀，如多次表示在忙、多次表示会考虑、多次表示稍后再说等。
20.用户询问免息券，优惠券的作用是什么，用户问题是：优惠券作用。免息券，优惠券使用期限，用户问题是：优惠券期限。
21.用户询问如何去查看免息券，优惠券，用户问题是如何查看优惠券。用户询问如何去使用免息券，优惠券，用户问题是如何使用优惠券。
22.用户担心借了之后还不起，借了之后造成信用损失，用户问题是还不起。
23.用户只有明确否定是本人，用户问题才是非本人。在核身阶段单纯的"你说"等，应是无问题，询问"你哪位"等是"询问什么平台"。
24.用户询问我们是不是机器人，用户问题是询问人还是机器人。
'''

    conversation_prompt = '''请记住，你是一名专业的销售员，你正通过电话和用户沟通，你需要根据对话记录、解决方案，销售技巧和用户情境，在解决方案和销售技巧备选话术中选择或组合最合适的话术回复用户。你主要的任务是回应并解决用户的问题，然后引导用户完成操作并全额借款，实现金条的销售转化。为了完成这个任务，你需要不断讲解活动、优惠券等给用户带来的权益，通过多轮对话激发用户用款需求，引导用户完成借款。

注意以下事项：
1.你的场景是电话销售，表达的内容足够通俗易懂简洁且口语化，随和、高中文化可理解，尽量表现出足够的亲和力和专业性。
2.如果用户当前有问题或疑问，在考虑解决用户问题的同时，也需要注意销售技巧的引导，特别是对话的初始阶段，一定要注意销售技巧的完整。当用户表示在忙、不需要等情况时，不要着急挂机，在不引起用户反感的情况下，继续进行多次劝服用户完成操作借款。
3.根据对话记录和当前的对话阶段做出回应,不要重复用户说过的信息,回复的内容不要有英文，不要有重复的卖点话术，要基有一定的变化，回复话术不要太长。只产生你的话术，不要产生用户的对话，每次只生成一个回复！把开头的"很抱歉"等句式过滤掉，非挂机结束语中不要表达诈骗相关的提醒。
4.当用户连续3次表达在忙、不需要、会考虑等、或者用户有投诉倾向时，先安抚客户再礼貌再见挂机。
5.生成的话术只能在解决方案话术集中选择一句话术或者在销售技巧话术集中选择一句话术，需要保障原有话术的完整性，不要无中生有，也不要过度生成。
6.回复的话术可以是候选集的一句话术，也可以将解决方案话术集中选择一条话术和销售技巧的一条话术进行拼接，但最终回复的话术只能有一个问题，不能同时出现两个问题，这样会使用户困扰。
7.对话的初始阶段(前6轮)，对于有疑问非投诉的用户，在回答用户问题的同时，也一定要使用销售技巧话术的拼接。
8.在"带操作-询问用户是否有App"时，先确认用户是否有京东App，没有京东APP再确认是否有京东金融App。
9.需要加用户微信时，先询问是否可以添加微信，用户同意则发送申请并结束再见，用户不同意则不发送申请并结束再见。    
'''
    for path in paths:
        datas = utils.jload(path)

        for data in datas:
            if '总结用户当前的问题' in data['conversations'][0]['value']:
                print(quesion_prompt + '\n===\n' +
                      data['conversations'][0]['value'].split('\n===\n')[1] + '\n===\n' +
                      data['conversations'][0]['value'].split('\n===\n')[2] )
                data['conversations'][0]['value'] = quesion_prompt + '\n===\n' + \
                                                    data['conversations'][0]['value'].split('\n===\n')[1] + '\n===\n' + \
                                                    data['conversations'][0]['value'].split('\n===\n')[2]

            '''if '话术的录音编号是' in data['conversations'][0]['value']:
                data['conversations'][0]['value'] = conversation_prompt + \
                                                    '用户问题：' + data['conversations'][0]['value'].split('用户问题：')[1]'''

        utils.jdump(datas, path)


def RLHF_data(path, target_path):

    insert_data = list()
    for file in os.listdir(path):
        if not file.endswith('.json') or file == 'badcase.json':
            continue
        data = utils.jload(path + file)

        for record in data:
            conversations = record['conversations']
            if len(conversations) == 2:
                prompt = conversations[0]['value']
                answer = conversations[1]['value']

                # ColossalAI
                '''pretrain_data = {
                    'instruction': prompt,
                    'input': '',
                    'output': answer,
                    'id': str(uuid.uuid1())
                }'''
                # deepspeed
                pretrain_data = {
                    'prompt': 'Human: '+prompt+'\n Assistant:',
                    'chosen': answer,
                }
                insert_data.append(pretrain_data)

    random.shuffle(insert_data)
    print('数据量:{}'.format(len(insert_data)))
    print(len(insert_data) // 10)
    train_data = insert_data[:len(insert_data) // 10 * 9]
    eval_data = insert_data[len(insert_data) // 10 * 9:]
    print(len(train_data))
    print(len(eval_data))

    utils.jdump(train_data, target_path + '_train.json')
    utils.jdump(eval_data, target_path + '_eval.json')


def transfer_dataTqwen():
    datas = utils.jload('../data_set/financial_data/result/all_train_data.json')

    for data in datas:
        data['conversations'][0]['from'] = 'user'
        data['conversations'][1]['from'] = 'assistant'

    utils.jdump(datas, '../data_set/financial_data/qwen_all_train.json')


def transfer_sentence_number():

    datas = utils.jload('../data_set/financial_data/result/all_train_data.json')

    result_datas = list()
    for data in tqdm.tqdm(datas):
        # print(data['id'])
        conversation_prompt = data['conversations'][0]['value']
        if '录音编号' in conversation_prompt and '解决方案话术集：' in conversation_prompt:
            original_question_utter_set = conversation_prompt.split('解决方案话术集：')[-1].split('会话历史')[0].split('销售技巧话术集：')[0]
            question_utter_set = original_question_utter_set.replace('\n', '').replace('\\n', '').replace('nan', "'无'").strip()

            # 将录音编号按照顺序替换成Ai，形成新的列表
            pre = 'A'
            question_final_set = list()
            question_sentence_number_dict = dict()
            for i, case in enumerate(eval(question_utter_set)):
                question_sentence_number_dict[case['录音编号']] = '{}{}'.format(pre, i)
                case['录音编号'] = '{}{}'.format(pre, i)

                knowledge = dict()
                for key in case:
                    if case[key] != '无':
                        knowledge[key] = case[key]
                question_final_set.append(knowledge)

            # 话术集中的录音编号替换成Ai
            # for sentence_number in question_sentence_number_dict:
            #     conversation_prompt = conversation_prompt.replace(sentence_number, question_sentence_number_dict[sentence_number])
            if question_final_set:
                conversation_prompt = conversation_prompt.replace(
                    original_question_utter_set.split('[')[-1].split(']')[0],
                    str(question_final_set))

            # 识别结果的录音编号
            if ',' in data['conversations'][1]['value']:
                transfer_number = list()
                for number in data['conversations'][1]['value'].split(','):
                    transfer_number.append(
                        question_sentence_number_dict[number] if number in question_sentence_number_dict else number)

                data['conversations'][1]['value'] = ','.join(transfer_number)
            else:
                number = data['conversations'][1]['value'].replace("'", '') if "'" in data['conversations'][1]['value'] \
                    else data['conversations'][1]['value']
                if number in question_sentence_number_dict:
                    data['conversations'][1]['value'] = question_sentence_number_dict[number]

            data['conversations'][0]['value'] = conversation_prompt

        if '录音编号' in conversation_prompt and '销售技巧话术集：' in conversation_prompt:
            original_skill_utter_set = conversation_prompt.split('销售技巧话术集：')[-1].split('会话历史')[0].split('解决方案话术集：')[0]
            skill_utter_set = original_skill_utter_set.replace('\n', '').replace('\\n', '').replace('nan', "'无'").strip()

            # 将录音编号按照顺序替换成Bi，形成新的列表
            pre = 'B'
            skill_final_set = list()
            skill_sentence_number_dict = dict()
            for i, case in enumerate(eval(skill_utter_set)):
                skill_sentence_number_dict[case['录音编号']] = '{}{}'.format(pre, i)
                case['录音编号'] = '{}{}'.format(pre, i)

                knowledge = dict()
                for key in case:
                    if case[key] != '无':
                        knowledge[key] = case[key]

                skill_final_set.append(knowledge)

            # 话术集中的录音编号替换成Bi
            # for sentence_number in skill_sentence_number_dict:
            #     conversation_prompt = conversation_prompt.replace(sentence_number, skill_sentence_number_dict[sentence_number])
            if skill_final_set:
                conversation_prompt = conversation_prompt.replace(
                    original_skill_utter_set.split('[')[-1].split(']')[0],
                    str(skill_final_set))

            # 识别结果的录音编号
            right_number = data['conversations'][1]['value'].replace("'", '') if "'" in data['conversations'][1]['value'] \
                else data['conversations'][1]['value']

            if ',' in right_number:
                transfer_number = list()
                for number in right_number.split(','):
                    transfer_number.append(
                        skill_sentence_number_dict[number] if number in skill_sentence_number_dict else number)

                data['conversations'][1]['value'] = ','.join(transfer_number)
            else:
                if right_number in skill_sentence_number_dict:
                    data['conversations'][1]['value'] = skill_sentence_number_dict[right_number]

        data['conversations'][0]['value'] = conversation_prompt

        result_datas.append(data)

    utils.jdump(result_datas, '../data_set/financial_data/result/final_train_data.json')


def decision_train_data():
    user_question_v0 = """
作为专业的、非常懂营销的信贷领域的{salesperson_role}，下面会有一段你与用户的对话记录，你的任务是根据与用户的对话来识别和判断用户当前的借款相关信息。用户信息列表可以包括以下几种：
1. 用户是否表述在忙：未知,表述在忙。
2. 用户是否表述不需要：未知,表述不需要。
注意事项:
1.输出格式为通过","拼接的value值，如：表述在忙,表述不需要，若只有一个有值，则返回该结果，空返回无
2.这是一个语音对话的场景，可能存在一些语音识别的错误，比如文本中的"那个"实际可能是"哪个"，你需要兼容这种错误正确识别出用户的信息。
3.未知以及未明确的信息均不用返回，返回的结果只包含信息的值，不要再包含任何其他内容。
4."@@quiet@@"代表的是用户未说话，没有任何含义，如果全篇用户未说话，直接输出无。
5.用户明确表示出忙才能认为用户表述在忙，不可以过度推理，没说就是没有。
对话记录如下：
===
{conversation_history}
===
根据对话记录和用户信息列表，一步一步推理出的用户信息：
"""

    wb = load_workbook('../customerInfo/data/2023-12-18.xlsx')
    ws = wb[wb.sheetnames[0]]

    num = 0
    datas = list()
    for i, row in enumerate(ws.values):
        if i != 0:
            dialogue = row[4]
            label = row[6]
            if len(dialogue) > 1000:
                num += 1
                continue

            data = {
                "id": str(uuid.uuid1()),
                "time": "2023-11-07 03:52:08",
                "task": "UserSituation",
                "model_name": "gpt-4",
                'conversations': [
                    {
                        'from': 'human',
                        'value': user_question_v0.format(salesperson_role='销售员', conversation_history=dialogue),
                    },
                    {
                        'from': 'gpt',
                        'value': label if label else '未知',
                    }
                ]
            }

            datas.append(data)
            if len(datas) == 1000:
                break

    print(num)
    print(len(datas))
    utils.jdump(datas, '../data_set/financial_data/train_data_files/decision_train_data.json')


def analysis_train_data():

    prompt_type_dict = {
        '选择其的下一步话术的录音编号是': 'conversation',
        '用一句短语简洁总结用户当前的问题': 'question',
        '根据对话记录和用户信息列表，一步一步推理出的用户信息': 'situation',
        '基于销售策略集合和对话记录，你推理出来接下来沟通的销售策略名是：': 'skill',
    }

    datas = utils.jload('../data_set/financial_data/result/final_train_data.json')

    train_data_prompt_num_dict = {
        'conversation': 0,
        'question': 0,
        'situation': 0,
        'skill': 0,
    }
    train_data_prompt_dict = {
        'conversation': dict(),
        'question': dict(),
        'situation': dict(),
        'skill': dict(),
    }
    for data in tqdm.tqdm(datas):

        conversation_prompt = data['conversations'][0]['value']
        result = data['conversations'][1]['value']

        for prompt_type in prompt_type_dict:
            if prompt_type in conversation_prompt:
                # train_data_prompt_dict[prompt_type_dict[prompt_type]] += 1
                if result not in train_data_prompt_dict[prompt_type_dict[prompt_type]]:
                    train_data_prompt_dict[prompt_type_dict[prompt_type]][result] = 0
                train_data_prompt_dict[prompt_type_dict[prompt_type]][result] += 1

                train_data_prompt_num_dict[prompt_type_dict[prompt_type]] += 1

    for key in train_data_prompt_dict:
        train_data_prompt_dict[key] = dict(sorted(train_data_prompt_dict[key].items(), key=lambda item: item[1], reverse=True))


    print(train_data_prompt_dict['conversation'])
    print(train_data_prompt_dict['question'])
    print(train_data_prompt_dict['situation'])
    print(train_data_prompt_dict['skill'])
    print(train_data_prompt_num_dict)


def train_data_update():

    company_id = 2144
    case_id_list = list()
    session_id_list = list()

    for session_id in session_id_list:
        pass



if __name__ == '__main__':
    now_date = datetime.now().strftime("%Y-%m-%d")
    # print(now_date)
    # file_path = "../data_set/bot_dialogue/bot_dialogue_" + now_date + "_成交_self.json"  # 文件夹路径
    # asyncio.run(mock_bot_dialogue_by_query(file_path))
    # asyncio.run(bot_dialogue_by_truth_conversation(["../deal_data/不能错case集.csv", ]))

    # file_path = ['../deal_data/2144_10_30_成交对话.csv', ]  # 文件夹路径
    # asyncio.run(mock_bot_dialogue_by_query(file_path))

    csv2vicuna(["../data_set/financial_data/raw_data/badcase_2401.csv", "../data_set/financial_data/raw_data/badcase.csv"],
               "../data_set/financial_data/train_data_files/badcase.json")
    merge_train_data("../data_set/financial_data/train_data_files/", "../data_set/financial_data/result/")
    transfer_sentence_number()

    # modify_train_data("../data_set/financial_data/raw_data/back/2023-11-02.json", "../data_set/financial_data/train_data_files/2023-11-02_info.json")
    # modify_train_data("../data_set/financial_data/raw_data/back/2023-11-03.json", "../data_set/financial_data/train_data_files/2023-11-03_info.json")

    # modify_csv_data("../data_set/financial_data/raw_data/badcase.csv",
    #                 "../data_set/financial_data/train_data_files/badcase.csv")
    # get_faq_corpus("/Users/cy/Downloads/222.csv")

    # check_train_data(['../data_set/financial_data/train_data_files/badcase.json'])

    # RLHF_data("../data_set/financial_data/train_data_files/", "../data_set/financial_data/result/sft")
    # csv2reward("../data_set/financial_data/raw_data/badcase.csv", "../data_set/financial_data/result/reward")
    # decision_train_data()

    # transfer_dataTqwen()


    # 训练数据随运行时间更新
    # 挂断驱动话术选择问题
    # 判别话术选择驱动逻辑，目前暂定为挂断率

    # train_data_update()
    # analysis_train_data()


